/*
 * DbHttpUserStore.cpp
 *
 *  Created on: 2018年2月9日
 *      Author: terry
 */

#include "DbHttpUserStore.h"
#include "TStringUtil.h"

DbHttpUserStore::DbHttpUserStore(std::shared_ptr< DataStore >& dbStore):
	m_dbStore(dbStore)
{
}

DbHttpUserStore::~DbHttpUserStore()
{
}

void DbHttpUserStore::setDb(std::shared_ptr< DataStore >& dbStore)
{
	m_dbStore = dbStore;
}

bool DbHttpUserStore::findUser(const std::string& username, HttpUser& user)
{
	return findLocal(username, user) || findDb(username, user);
}

bool DbHttpUserStore::findDb(const std::string& username, HttpUser& user)
{
	if (!m_dbStore)
	{
		return false;
	}

	std::string sql = comn::StringUtil::format("select password from user where username='%s'", username.c_str());
	std::string password;
	if (!m_dbStore->query(sql, password))
	{
		return false;
	}

	user.username = username;
	user.password = password;

	return true;
}
